package com.br.command.impl;

import java.io.IOException;
import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONObject;

import com.br.command.BaseCommand;
import com.br.socket.ConnectionWS;
import com.br.socket.GameSocket;
import com.br.utils.BufferUtils;
import com.br.utils.TiposMensagensEnviar;

public class BuscarPlayersTela implements BaseCommand{

	private Logger logger = Logger.getLogger(this.getClass());
	
	public void process(ConnectionWS me, JSONObject json) {
		try{
			List<ConnectionWS> connections = GameSocket.getConnections();
			synchronized (connections) {
				for (int i = 0; i < connections.size(); i++) {
					try {
						ConnectionWS con = connections.get(i);
						
						if(!con.getId().equals(me.getId())){
							JSONObject playerJson = BufferUtils.montarPlayer(TiposMensagensEnviar.OTHER_PLAYER_ON_SCREEN, con.getPlayer());
							me.sendMessage(playerJson.toString());
						}
					
					} catch (IOException e) {
						logger.error("Erro em BuscarPlayersTela",e);
					}
					
				}
			}
		}catch(Exception e){
			logger.error("Erro em BuscarPlayersTela",e);
		}
	}

}
